#include <iostream>
#include <list>
int main()
{
    std::list<int> test = {0};  // 定义list对象,初始化为0

    test.push_back(1);  
    test.push_back(2);  
    test.push_back(3);  
   
     std::cout<<"遍历输出:"<<std::endl;
    for(auto i = test.begin();i != test.end();i++) //迭代器遍历输出
    {
        std::cout<<*i<<" ";
    }
    
   auto it = test.begin()++;//在第二位置插入元素4
    it = test.insert(it,4);//返回插入位置的迭代器
    std::cout<<"\n插入元素后:"<<std::endl;
    for(auto i = test.begin();i != test.end();i++) //遍历输出,应该是0,4,1,2,3
    {
        std::cout<<*i<<" ";
    }

    auto last = test.end();
    --last;  // 移动到最后一个元素（值为3）
    it = test.erase(last);  // 删除后列表变为 [0,4,1,2]
    std::cout << "\n删除元素后:" << std::endl;
    for (auto i = test.begin(); i != test.end(); i++) {
        std::cout << *i << " ";  // 输出：0 4 1 2
    }
    return 0;
}